<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="debounce">debounce</button>
    <button id="throttle">throttle</button>
    <script>
        let button1 = document.querySelector('#debounce');
        let button2 = document.querySelector('#throttle');
        button1.onclick = debounce(()=>{
            console.log('debounce');
        },1000);
        button2.onclick = throttle(()=>{
            console.log('throttle');
        },1000);

        function debounce(fun,time){
            let timer = null;
            return function(){
                if(timer!=null){
                    clearTimeout(timer);
                }
                timer = setTimeout(() => {
                    fun();
                }, time);
            }
        }

        function throttle(fun,time){
            let now = Date.now();
            return function(){
                if(Date.now()-now>=time){
                    now = Date.now();
                    fun();
                }
            }
        }
    </script>
</body>

</html>